﻿using AutoMapper;
using BackData;
using Domain;
using Infratructure;
using MediatR;
using Rabbit.API.Application.Command.FinishStep;
using Rabbit.API.DTOs;

namespace Rabbit.API.Application.Handler.FinishStep
{
    public class FinishStepGetHandler:IRequestHandler<FinishStepGetCommand,APIResult<FinishStepDTO>>
    {
        private readonly IRepository<FinishStepModel> finishstepway;
        private readonly IMapper mapper;
        private readonly ILogger<FinishStepGetHandler> log;

        public FinishStepGetHandler(IRepository<FinishStepModel> finishstepway,IMapper mapper,ILogger<FinishStepGetHandler> log)
        {
            this.finishstepway = finishstepway;
            this.mapper = mapper;
            this.log = log;
        }
        /// <summary>
        /// 获取深加工详情
        /// </summary>
        /// <param name="request">主键</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>详情</returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<FinishStepDTO>> Handle(FinishStepGetCommand request, CancellationToken cancellationToken)
        {
            APIResult<FinishStepDTO> res = new APIResult<FinishStepDTO>();
            try
            {
                var value =await finishstepway.GetValue(request.Id);
              
                if (value != null)
                {
                    var result = mapper.Map<FinishStepDTO>(value);
                    log.LogInformation($"获取深加工详情成功");
                    res.Message = "获取深加工详情成功";
                    res.Num = StateCode.OK;
                    res.Data = result;
                }
                else
                {
                    log.LogError($"获取深加工详情失败");
                    res.Message = "获取深加工详情失败";
                    res.Num = StateCode.Fail;
                }
                return res;
            }
            catch (Exception ex)
            {
                log.LogError($"获取深加工详情发生异常{ex.Message}");
                throw;
            }
        }
    }
}
